package file;

/**
 * Author: mb25428
 * Create Date:  3/23/2013
 * 
 * Comments: this java class is for the video encrypt and decryption
 * JDK version: <JDK 1.7>
 * 
 * Modified By: mb25428
 * Modified Date: 4/3/2013
 * Why&What is Modified: Add some image enhance controller
 * 
 */

import java.io.File;
import java.io.InputStream;
import java.security.NoSuchAlgorithmException;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

import common.VideoBean;
import common.UserBean;

public class VideoEncryptController {
	
	private StreamedContent enhanceVideo;
	private SecretKey skey;
	
	public SecretKey getSkey() {
		return skey;
	}

	public void setSkey(SecretKey skey) {
		this.skey = skey;
	}

	public StreamedContent getEnhanceVideo() {
		return enhanceVideo;
	}

	public void setEnhanceVideo(StreamedContent enhanceVideo) {
		this.enhanceVideo = enhanceVideo;
	}

	public void enhanceVideo(){
		HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);
		FileUploadController f = (FileUploadController) session.getAttribute("fileUploadController");

		UserBean userBean = f.getUserBean();
		VideoBean videoBean = userBean.getOriVideo();
		String oriPath = videoBean.getWholePath();
		
		String ehancePath = userBean.getOriVideo().getFilePath() + File.separator+"enhance" + userBean.getOriVideo().getVideoName();
		
		String enhanceVideoPath ="upload" + File.separator+"enhance" + userBean.getOriVideo().getVideoName();
		Runtime rn = Runtime.getRuntime();
		Process p = null;
		String commond = "";
		commond += "cmd /c start D:\\video\\VideoEnhancer.exe ";
		commond += " -i ";
		commond += oriPath;
		commond += " -o ";
		commond += ehancePath;
		commond += " -d 640 320 -vc Huff -fast ";
		
		f.setEnhanceVideoPath(enhanceVideoPath);
		
		userBean.setEnchancePath(enhanceVideoPath);
		
		System.out.println(commond);
		
		try {
			p = rn.exec(commond);
			//p = rn.exec("cmd /c start D:\\video\\VideoEnhancer.exe -i D:\\wang.avi -o D:\\176qq.avi -d 640 480 -vc Huff -fast");
		} catch (Exception e) {
			System.out.println("Error exec!");
		}
	
		
	}
	
	public void downEnhanceVideo(){
		HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);
		FileUploadController f = (FileUploadController) session.getAttribute("fileUploadController");

		UserBean userBean = f.getUserBean();
		InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream("/upload/"+"enhance"+userBean.getOriVideo().getVideoName()); 
		enhanceVideo = new DefaultStreamedContent(stream, "avi", "enhance"+userBean.getOriVideo().getVideoName());
	}
	
	public void encryptVideo() throws Exception{
		HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);
		FileUploadController f = (FileUploadController) session.getAttribute("fileUploadController");

		UserBean userBean = f.getUserBean();
		VideoBean videoBean = userBean.getOriVideo();
		
		File video = new File(videoBean.getWholePath());
		KeyGenerator kgen;
		try {
			kgen = KeyGenerator.getInstance("AES");
			SecretKey skey = kgen.generateKey();
			File outFile = VideoProcess.encryptVideo(video,skey);
			
			VideoProcess.decrptVideo(outFile,skey);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
        //byte key[] = {0x00,0x32,0x22,0x11,0x00,0x00,0x00,0x00,0x00,0x23,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
        
	}
	
	public void encryptVideo1(){
		System.out.println("123 encrypt");
		HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);
		FileUploadController f = (FileUploadController) session.getAttribute("fileUploadController");
		UserBean userBean = f.getUserBean();
		
		String wholePath = userBean.getOriVideo().getWholePath();
		System.out.println(wholePath);
		String enWholePath = userBean.getOriVideo().getFilePath() + File.separator+"enhance" + userBean.getOriVideo().getVideoName();
		
		
		File oriFile = new File(userBean.getOriVideo().getWholePath());
		File outFile = new File(enWholePath);
//		File deFile = new File(deWholePath);
		
		try {
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
	        //byte key[] = {0x00,0x32,0x22,0x11,0x00,0x00,0x00,0x00,0x00,0x23,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
	        skey = kgen.generateKey();
	        System.out.println(skey.toString());
			VideoProcess.encryptVideo(oriFile,skey,outFile);
			userBean.setEnchancePath("upload" + File.separator+"enhance" + userBean.getOriVideo().getVideoName());
			
			System.out.println("encrypt success");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public void decryptVideo(){
		System.out.println("123 decrypt");
		HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);
		FileUploadController f = (FileUploadController) session.getAttribute("fileUploadController");
		UserBean userBean = f.getUserBean();
		
		String wholePath = userBean.getOriVideo().getWholePath();
		System.out.println(wholePath);
		String deWholePath = userBean.getOriVideo().getFilePath() + File.separator+"enhance" + userBean.getOriVideo().getVideoName();
		
		File oriFile = new File(userBean.getOriVideo().getWholePath());
		//File outFile = new File(enWholePath);
		File deFile = new File(deWholePath);
		
		try {
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
	        //byte key[] = {0x00,0x32,0x22,0x11,0x00,0x00,0x00,0x00,0x00,0x23,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
			
			VideoProcess.decrptVideo(oriFile,skey,deFile);
			
			userBean.setEnchancePath("upload" + File.separator+"enhance" + userBean.getOriVideo().getVideoName());
			System.out.println("decrypt success");
			System.out.println();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
}
